#include "addfrienddialog.h"

/////////////////////////////////////
/// 添加好友对话框
/////////////////////////////////////

AddFriendDialog::AddFriendDialog(QWidget* parent)
    :QDialog(parent)
{
    // 设置基本属性
    this->setWindowTitle("添加好友");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setFixedSize(500, 600);
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setStyleSheet("QDialog { background-color:rgb(255, 255, 255); }");

    // 布局管理器
    glayout = new QGridLayout();
    glayout->setSpacing(10);
    glayout->setContentsMargins(20, 20, 20, 20);
    this->setLayout(glayout);

    // 搜索框
    lineEdit = new QLineEdit();
    lineEdit->setFixedHeight(40);
    lineEdit->setPlaceholderText("按手机号/用户序号/昵称搜索");
    lineEdit->setStyleSheet("QLineEdit { color:rgb(50, 50, 50); border-radius:10px; "
                            "border:none; font-size:15px; padding-left:10px; "
                            "background-color:rgb(230, 230, 230); }");

    // 搜索按钮
    QPushButton* searchBtn = new QPushButton();
    searchBtn->setIcon(QIcon(":/resource/image/search.png"));
    searchBtn->setFixedSize(40, 40);
    searchBtn->setIconSize(QSize(20, 20));
    searchBtn->setStyleSheet("QPushButton:hover { background-color:rgb(210, 210, 210); }"
                             "QPushButton:pressed { background-color:rgb(200, 200, 200); }"
                             "QPushButton { background-color:rgb(230, 230, 230); "
                             "border:none; border-radius:10px; }");

    // 添加到布局管理器
    glayout->addWidget(lineEdit, 0, 0, 1, 8);
    glayout->addWidget(searchBtn, 0, 8, 1, 1);

    // 滚动区域
    initResultArea();
}

/////////////////////////////////////
/// 搜索结果中的每一项
/////////////////////////////////////

void AddFriendDialog::initResultArea()
{
    // 滚动区域
    QScrollArea* scroll = new QScrollArea();
    scroll->setStyleSheet("QScrollArea { border:none; }");
    scroll->setWidgetResizable(true);
    scroll->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal { height:0px; }");
    scroll->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width:2px; }");
    glayout->addWidget(scroll, 1, 0, 1, 9);

    container = new QWidget();
    container->setStyleSheet("QWidget { background-color:rgb(255, 255, 255); }");
    scroll->setWidget(container);

    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setSpacing(0);
    vlayout->setContentsMargins(0, 0, 0, 0);
    container->setLayout(vlayout);

    // 添加测试数据
#if TEST_F
    for(int i = 0;i < 20;i ++) {
        model::UserInfo* info = new model::UserInfo();
        info->userId = "1234" + QString::number(i);
        info->avatar = QIcon(":/resource/image/default_avatar.png");
        info->description = "成为海贼王的男人" + QString::number(i);
        info->nickname = "路飞" + QString::number(i);
        info->phone = "1234567890";
        this->addResult(*info);
    }
#endif
}

void AddFriendDialog::addResult(const model::UserInfo &userInfo)
{
    FriendResultItem* item = new FriendResultItem(userInfo);
    container->layout()->addWidget(item);
}

void AddFriendDialog::clear()
{
    QVBoxLayout* layout = dynamic_cast<QVBoxLayout*>(container->layout());
    for(int i = layout->count() - 1;i >= 0;i --) {
        QLayoutItem* item = layout->takeAt(i);
        if(item == nullptr || item->widget() == nullptr) {
            continue;
        }
        delete item->widget();
        delete item;
    }
}

void AddFriendDialog::setText(const QString &text)
{
    lineEdit->setText(text);
}

FriendResultItem::FriendResultItem(const model::UserInfo &userInfo)
    :userInfo(userInfo)
{
    // 设置基本属性
    this->setFixedHeight(70);

    // 布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setHorizontalSpacing(10);
    layout->setVerticalSpacing(0);
    layout->setContentsMargins(0, 0, 20, 0);
    this->setLayout(layout);

    // 用户头像
    QPushButton* avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(40, 40);
    avatarBtn->setIcon(userInfo.avatar);
    avatarBtn->setIconSize(QSize(40, 40));

    // 用户昵称
    QLabel* nameLabel = new QLabel();
    nameLabel->setText(userInfo.nickname);
    nameLabel->setFixedHeight(35);
    nameLabel->setStyleSheet("QLabel { color:rgb(25, 25, 25); font-size:15px; font-weight:600; }");

    // 用户签名
    QLabel* descLabel = new QLabel();
    descLabel->setText(userInfo.description);
    descLabel->setFixedHeight(35);
    descLabel->setStyleSheet("QLabel { color:rgb(25, 25, 25); font-size:15px;}");

    // 添加好友按钮
    QPushButton* addFriendBtn = new QPushButton("添加好友");
    addFriendBtn->setFixedSize(100, 35);
    addFriendBtn->setStyleSheet("QPushButton { background-color:rgb(20, 233, 120); "
                                "color:rgb(20, 20, 20); border-radius:5px; }"
                                "QPushButton:hover { background-color:rgb(210, 210, 210); } "
                                "QPushButton:pressed { background-color:rgb(190, 190, 190); }");

    // 添加到布局管理器中
    layout->addWidget(avatarBtn, 0, 0, 2, 1);
    layout->addWidget(nameLabel, 0, 1, 1, 1);
    layout->addWidget(descLabel, 1, 1, 1, 1);
    layout->addWidget(addFriendBtn, 0, 2, 2, 1);
}
